﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TB.BLL
{
    public class MpRespMsg
    {
        private DALFactory.DALFactory dataFactory = DataProvider.DefaultProvider;

        public int InsertRespMsg(Entity.MpRespMsg respMsg)
        {
            return dataFactory.DALMpRespMsgProvider.Insert(respMsg);
        }

        public int DeleteRespMsg(int id_auto)
        {
            TB.Entity.MpRespMsg resp = new Entity.MpRespMsg();
            resp.id_auto = id_auto;
            return dataFactory.DALMpRespMsgProvider.Delete(resp);
        }

        public int GetIdAuto(string keyword,out string createTime)
        {
            List<TB.Entity.MpRespMsg> list = dataFactory.DALMpRespMsgProvider.LoadAll(keyword);
            if (list.Count == 0)
            {
                createTime = "";
                return 0;
            }
            else
            {
                createTime = list[0].r2.ToString();
                return list[0].id_auto;
            }
        }

        public int UpdateRespMsg(TB.Entity.MpRespMsg resp)
        {
            return dataFactory.DALMpRespMsgProvider.Update(resp);
        }

        public int UpdateColValue(string colName, string value, int id_auto)
        {
            return dataFactory.DALMpRespMsgProvider.Update(colName, value, id_auto);
        }

        /// <summary>
        /// 关键词是否存在
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns>关键词不存在返回""，存在则返回关键词组合的文本内容消息(带有换行符)</returns>
        public string GetRespTextMessage(string keyword)
        {
            List<Entity.MpRespMsg> list = LoadAllByKeywordAndMsgType(keyword, "text");
            StringBuilder buffer = new StringBuilder();  
            string retStr = "";
            if (list.Count == 0)
            {
                return retStr;
            }
            else
            {
                foreach (Entity.MpRespMsg resp in list)
                {
                    buffer.Append(resp.text_Content+"  "+resp.r3).Append("\n");
                }
                retStr = buffer.ToString().TrimEnd('\n');
            }
            return retStr;
        }

        public string GetWxMenuStr()
        {
            StringBuilder buffer = new StringBuilder();
            string menuFile =HttpContext.Current.Server.MapPath("wxMenu.txt");
            System.IO.StreamReader sr = new System.IO.StreamReader(menuFile);
            while (!sr.EndOfStream)
            {
                string temp=sr.ReadLine();
                buffer.Append(temp).Append("\n");
            }
            sr.Close();
            return buffer.ToString().TrimEnd('\n');
        }

        public Dictionary<string,string> GetRespMusicMessage(string keyword)
        {//对于音乐返回只能有一条数据。数据库约束时不能对应多个关键词
            Dictionary<string, string> musicItem = new Dictionary<string, string>();
            List<Entity.MpRespMsg> list = LoadAllByKeywordAndMsgType(keyword, "music");
            musicItem["Title"] = list[0].news_Title;
            musicItem["Description"] = list[0].news_Description;
            musicItem["MusicUrl"] = list[0].music_MusicUrl;
            musicItem["HQMusicUrl"] = list[0].music_HQMusicUrl;
            return musicItem;
        }

        /// <summary>
        /// 关键词是否存在于数据库中
        /// </summary>
        /// <param name="keyword">用户发送的关键词</param>
        /// <returns>存在返回true，不存在返回false</returns>
        public bool IsKeywordExist(string keyword,List<string> listMsgType)
        {
            List<Entity.MpRespMsg> list = dataFactory.DALMpRespMsgProvider.LoadAll(keyword);
            if (list.Count == 0)
            {
                return false;
            }
            else
            {
                foreach (Entity.MpRespMsg temp in list)
                {
                    if (!listMsgType.Contains(temp.msgType))
                    {
                        listMsgType.Add(temp.msgType);
                    }
                }
                return true;
            }
        }

        /// <summary>
        /// 返回数据库中查到的所有关键词信息
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <param name="msgType">消息类型</param>
        /// <returns>返回消息列表集合</returns>
        public List<Entity.MpRespMsg> LoadAllByKeywordAndMsgType(string keyword,string msgType)
        {
            //此处要考虑数据库的约束，可能出的情况是如 "33"这个关键词同时存在于text,music,news消息中
            //但回复消息时只能回复一种消息。因此在数据库添加数据数据时要考虑到。如果某关键词同时存在
            //于多个消息字段中优先返回news消息。
            List<Entity.MpRespMsg> retList = dataFactory.DALMpRespMsgProvider.LoadAll(keyword);
            List<Entity.MpRespMsg> list = new List<Entity.MpRespMsg>(); //过滤类型，有news，text,music三种类型
            foreach (Entity.MpRespMsg temp in retList)
            {
                if (temp.msgType == msgType)
                    list.Add(temp);
            }
            return list;
        }


        /// <summary>
        /// 返回NewsMessage信息
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <returns>当关键词不存在时返回null，否则返回相应信息</returns>
        public List<Dictionary<string, string>> GetRespNewsMessage(params string[] keyword)
        {
           // List<Entity.MpRespMsg> listItem = new List<Entity.MpRespMsg>(); //接收从数据库中返回的List数据，可能有多条
            List<Entity.MpRespMsg> list=new List<Entity.MpRespMsg>();//将接收来的List存入其中，每条记录为List<Entity.MpRespMsg>类型。 
            foreach (string strKeyWord in keyword)
            {
                //listItem也可能有多条存在。
                List<Entity.MpRespMsg> tempList = LoadAllByKeywordAndMsgType(strKeyWord, "news");
                foreach(Entity.MpRespMsg temp in tempList)
                {
                    list.Add(temp);
                }
            }
            List<Dictionary<string, string>> artItemList = new List<Dictionary<string, string>>(); ;
            if (list.Count == 0)
            {
                return null;
            }
            else
            {
                int index = 0;//用来标识是否是第一条消息，第一条消息为大图，其余为缩略图
                foreach (Entity.MpRespMsg newsMsg in list)
                {
                    Dictionary<string, string> artItemDic = new Dictionary<string, string>();
                    artItemDic.Add("Title", newsMsg.news_Title);
                    artItemDic.Add("Description", newsMsg.news_Description);
                    artItemDic.Add("PicUrl",(index==0)?newsMsg.news_PicUrl:newsMsg.r3);
                    artItemDic.Add("Url", newsMsg.news_Url);
                    artItemList.Add(artItemDic);
                    index++;
                }
                return artItemList;
            }
        }

        //可以定义为一个公共的类
        public System.Data.DataSet GetRespMsgDT()
        {
            string strSql = "select *from MpRespMsg";
            return dataFactory.DALMpRespMsgProvider.GetDataSet(strSql);
        }

        public System.Data.DataSet GetRespMsgDT(string strSql)
        {
           //string strSql = "select *from MpRespMsg";
            return dataFactory.DALMpRespMsgProvider.GetDataSet(strSql);
        }
    }
}
